package com.vrv.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.thoughtworks.xstream.XStream;
import com.vrv.entity.DbInfo;

/**
 * @author hongliang.dinghl DOM生成与解析XML文档
 */
public class XMLRead {

    public static String getFilepath() {
        String tmp = XMLRead.class.getClassLoader().getResource("/").getPath();
        String path = tmp.substring(0, tmp.lastIndexOf("/webapps/")) + "/webapps/dbconfig/";
        return path;
    }

    public static DbInfo xmlDbInfo() {
        DbInfo dbInfo = null;
        try {
            String path = getFilepath();
            if (!StringUtils.isEmpty(path)) {
                path += "bigDataConfig.xml";
            }
            File file2 = new File(path);
            FileInputStream in = new FileInputStream(file2);

            XStream xstream2 = new XStream();
            xstream2.alias("bigdata", DbInfo.class);
            dbInfo = (DbInfo) xstream2.fromXML(in);
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());
        }
        if (dbInfo == null) {
            dbInfo = new DbInfo();
        }
        return dbInfo;
    }

    public static String getHost(HttpServletRequest request) {
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url
                .delete(url.length() - request.getRequestURI().length(), url.length())
                .delete(0, url.indexOf("//") + 2).toString();
        return tempContextUrl;
    }

    public static String AIM = SystemConfig.getStr("restful_ip");

    public static String HND_IP = SystemConfig.getStr("hnd_ip");
}
